package com.feifan.main.controller;


import com.api.main.dto.RedPacketDto;
import com.api.main.enums.CodeStatus;
import com.api.main.response.BaseResponse;
import com.api.main.service.IRedService;
import org.mybatis.logging.Logger;
import org.mybatis.logging.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import sun.rmi.runtime.Log;

import javax.print.attribute.standard.Media;
import java.math.BigDecimal;


/**
 * @program: GetRegBag
 * @author: Xiaofan
 * @createTime: 2021-07-11 21:12
 * @description: Functions of this class is 红包控制器
 **/
@RestController
@CrossOrigin //防止自己的ip地址与端口号冲突的一个注解
public class RedController {
    //定义一个日志
    private static final Logger logger= LoggerFactory.getLogger(RedController.class);

    //定义请求功能路径的一个前缀
    private static final String prefix="red/packet";

    @Autowired
    private IRedService redService;

    @PostMapping(value = {prefix+"/hand/out"},
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    //consumes右边的数据代表传送数据为application/json格式带utf8的
    //发红包
    private BaseResponse handOut(@Validated//该注解用于校验数据
                                 @RequestBody RedPacketDto dto, BindingResult result){
        if(result.hasErrors()){
            return new BaseResponse<>(CodeStatus.InvalidParams);//把产生的错误的类型
        }
        //没有产生参数错误
        BaseResponse baseResponse=new BaseResponse(CodeStatus.Success);

        redService.handOut(dto);//发红包

        String redId=redService.handOut(dto);
        baseResponse.setData(redId);//把红包的id传到前端

        return baseResponse;
    }

    @PostMapping(value = {prefix+"/hand/rob"})
    //抢红包
    private BaseResponse rob(@RequestParam String userId,@RequestParam String redId){
        BaseResponse baseResponse=new BaseResponse(CodeStatus.Success);
        BigDecimal bigDecimal=redService.rob(userId,redId);
        baseResponse.setData(bigDecimal);
        return baseResponse;
    }

}